package com.rh.manage.service;

import cn.hutool.core.util.RuntimeUtil;
import com.rh.manage.annotation.ComponentBean;
import com.rh.manage.config.ReadUserConfig;
import com.rh.manage.constants.FrameCategoryEnum;
import com.rh.manage.model.UserConfig;
import com.rh.manage.view.GitProjectPathTextFieldView;
import com.rh.manage.view.OpenMindAppCmdTextFieldView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@ComponentBean(FrameCategoryEnum.PRO_MANAGE)
public class MainFrameService {

    @Autowired
    private ReadUserConfig readUserConfig;
    @Autowired
    private OpenMindAppCmdTextFieldView openMindAppCmdTextFieldView;
    @Autowired
    private GitProjectPathTextFieldView gitProjectPathTextFieldView;

    /**
     * 打开思维导图app
     */
    public void openMindApp() {
        UserConfig userConfig = readUserConfig.getUserConfig();
        if (userConfig == null || StringUtils.isBlank(userConfig.getMindAppCmd())) {
            return;
        }
        RuntimeUtil.execForStr("sh -c \"" + userConfig.getMindAppCmd() + "\"");
    }

    /**
     * 保存配置
     */
    public void saveConfig() {
        // 获得编辑框1的内容
        String openMindAppCmd = openMindAppCmdTextFieldView.getText();
        // 获得编辑框2的内容
        String gitProjectPath = gitProjectPathTextFieldView.getText();
        // 保存到config.json文件
        readUserConfig.setMindAppCmd(openMindAppCmd);
        readUserConfig.setGitProjectPath(gitProjectPath);
        readUserConfig.writeToFile();
    }
}
